package com.netease.nie.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.netease.nie.base.exceptions.BusinessException;
import com.netease.nie.base.utils.DateUtils;
import com.netease.nie.constants.BusinessConstants;
import com.netease.nie.constants.DeletedFlagConstants;
import com.netease.nie.constants.PutOnTimeTypeConstants;
import com.netease.nie.mapper.ExchangeGoodsInfoMapper;
import com.netease.nie.model.ExchangeGoodsInfo;

@Repository
public class ExchangeGoodsInfoDao {

	@Resource
	private ExchangeGoodsInfoMapper exchangeGoodsInfoMapper;
	
	public ExchangeGoodsInfo queryGoodsInfoById(int id) {
		ExchangeGoodsInfo exchangeGoodsInfo = exchangeGoodsInfoMapper.queryExchangeGoodsInfoById(id);
		if (exchangeGoodsInfo == null)
			throw new BusinessException(BusinessConstants.GOODS_NOT_EXISTS.getCode(), "商品不存在");
		
		if (exchangeGoodsInfo.getIsDeleted() == DeletedFlagConstants.DELETED.getCode())
			throw new BusinessException(BusinessConstants.GOODS_HAD_DELETED.getCode(), "商品已删除");
		
		long currTime = DateUtils.currentTimeInMills();
		if (exchangeGoodsInfo.getPutOnTimeType() == PutOnTimeTypeConstants.PUT_ON_PENDING.getCode()) {
			if (currTime > exchangeGoodsInfo.getPutOffTime())
				throw new BusinessException(BusinessConstants.GOODS_HAD_BEEN_PUT_OFF.getCode(), "商品已下线");
		}
		else if (exchangeGoodsInfo.getPutOnTimeType() == PutOnTimeTypeConstants.PUT_ON_IMMEDIATELY.getCode()) {
			if (currTime > exchangeGoodsInfo.getPutOffTime())
				throw new BusinessException(BusinessConstants.GOODS_HAD_BEEN_PUT_OFF.getCode(), "商品已下线");
		}
		else if (exchangeGoodsInfo.getPutOnTimeType() == PutOnTimeTypeConstants.PUT_ON_PREPARING.getCode()) {
			if (currTime < exchangeGoodsInfo.getPutOnTime() || currTime > exchangeGoodsInfo.getPutOffTime())
				throw new BusinessException(BusinessConstants.GOODS_HAD_BEEN_PUT_OFF.getCode(), "商品已下线");
		}
		return exchangeGoodsInfo;
	}
	
	public List<ExchangeGoodsInfo> queryGoodsInfoListByColumnInfoId(int columnId, int pageIndex, int pageSize) {
		return exchangeGoodsInfoMapper.queryGoodsInfoListByColumnInfoId(columnId, pageIndex, pageSize);
	}
	
	public List<ExchangeGoodsInfo> queryGoodsInfoListByPrivilegeLevel(int level) {
		return exchangeGoodsInfoMapper.queryGoodsInfoListByPrivilegeLevel(level);
	}
	
	public List<ExchangeGoodsInfo> queryGoodsInfoListByColumnInfoIds(List<Integer> columnInfoIds) {
		return exchangeGoodsInfoMapper.queryGoodsInfoListByColumnInfoIds(columnInfoIds);
	}
	
	public int deductGoodsStock(int goodsId) throws Exception {
		return exchangeGoodsInfoMapper.deductGoodsStock(goodsId);
}
	public List<ExchangeGoodsInfo> queryGoodsInfoListByIds(List<Integer> ids) {
		return exchangeGoodsInfoMapper.queryExchangeGoodsInfoListByIds(ids);
	}
		
	/**
	 * 查询商品信息，不需要判断上下架状态，用于个人订单查询
	 * @param id
	 * @return
	 */
	public ExchangeGoodsInfo queryGoodsInfoByIdWithoutStatus(int id) {
		ExchangeGoodsInfo exchangeGoodsInfo = exchangeGoodsInfoMapper.queryExchangeGoodsInfoById(id);
		if (exchangeGoodsInfo == null)
			throw new BusinessException(BusinessConstants.GOODS_NOT_EXISTS.getCode(), "商品不存在");
		
/*		if (exchangeGoodsInfo.getIsDeleted() == DeletedFlagConstants.DELETED.getCode())
			throw new BusinessException(BusinessConstants.GOODS_HAD_DELETED.getCode(), "商品已删除");*/
		return exchangeGoodsInfo;
	}
}
